﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Database
{
    public class DatabaseClass
    {
        public struct User
        {
            public int id;
            public string username;
            public string password;
            public string firstName;
            public string lastName;
            public string sex;
            public string type;

         
        };

        public struct Tourney
        {
            public int jumpHeight;
            public int totalDivers;
            public int totalJudges;
            public string tourneyName;
            public string sex;
        };

        public static int CompareList(User x, User y)   
    {
        return x.id.CompareTo(y.id);
    }
        

        
        public List<User> ReadUsersFromFile(ref List<User> Users)
        {
            User Login = new User();
            string line = "";
            using (StreamReader sr = new StreamReader("login.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    if (line == "")
                        break;
                    string[] split = line.Split(' ');
                    Login.id = Convert.ToInt32(split[0]);
                    Login.username = split[1];
                    Login.password = split[2];
                    Login.firstName = split[3];
                    Login.lastName = split[4];
                    Login.sex = split[5];
                    Login.type = split[6];
                    Users.Add(Login);
                }
                sr.Close();
                Users.Sort(DatabaseClass.CompareList);
                return Users;
            }
            
        }
        public int DeleteUser(string username, ref List<User> Users)
        {
            bool usernameFound = false;
            Users.Clear();
            ReadUsersFromFile(ref Users);
            for (int i = 0; i < Users.Count(); i++)
            {
                if (username == Users[i].username)
                {
                    Users.RemoveAt(i);
                    usernameFound = true;
                    break;
                }
            }
            if (usernameFound == false)
            {
                return 0;
            }
            Users.Sort(DatabaseClass.CompareList);
            File.Delete("login.txt");
            
            for (int i = 0; i < Users.Count(); i++)
            {
                File.AppendAllText("login.txt", Users[i].id.ToString() + " " + Users[i].username + " " + Users[i].password + " "
                        + Users[i].firstName + " " + Users[i].lastName + " " + Users[i].sex + " " + Users[i].type + Environment.NewLine);
            }
            return 1;
        }
        public int RegisterUser(string username, string password, string firstName,
            string lastName, string sex, string type, ref List<User> Users)
        {
            Users.Clear();
            ReadUsersFromFile(ref Users);
            bool UniqueIDFound = false;
            int id = 0;
            User Login = new User();
            for (int i = 0; i < Users.Count(); i++)
            {
                if (username == Users[i].username)
                {
                    return 0;
                }
            }
            while (UniqueIDFound == false)
            {
                id++;
                for (int j = 0; j < Users.Count(); j++)
                {
                    if (id == Users[j].id)
                    {
                        UniqueIDFound = false;
                        break;
                    }
                    else if ((j == Users.Count()-1) & (UniqueIDFound == false))
                    {
                        UniqueIDFound = true;
                        break;
                    }
                    
                }
                
                
            }
            Login.id = id;
            Login.username = username;
            Login.password = password;
            Login.firstName = firstName;
            Login.lastName = lastName;
            Login.sex = sex;
            Login.type = type;
            Users.Add(Login);
            Users.Sort(DatabaseClass.CompareList);
            
            File.AppendAllText("login.txt", id.ToString() + " " + username + " " + password + " "
                + firstName + " " + lastName + " " + sex + " " + type + Environment.NewLine );
            
           
            
            Users.Clear();
            
          
            return 1;
            
        }
        public void CreateTourney(int jumpHeight, int totalDivers, int totalJudges, string sex, string name)
        {
            File.AppendAllText("tourney.txt", name + ";" + jumpHeight + ";" + totalDivers + ";"
                + totalJudges + ";" + sex + Environment.NewLine);
        }

        public List<Tourney> ReadTourneyFromFile(ref List<Tourney> Tourneys)
        {
            Tourneys.Clear();
            Tourney Competition = new Tourney();
            string line = "";
            using (StreamReader sr = new StreamReader("tourney.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    string[] split = line.Split(';');
                    Competition.tourneyName = split[0];
                    Competition.jumpHeight = Convert.ToInt32(split[1]);
                    Competition.totalDivers = Convert.ToInt32(split[2]);
                    Competition.totalJudges = Convert.ToInt32(split[3]);
                    Competition.sex = split[4];
                    Tourneys.Add(Competition);
                }
                return Tourneys;
            }
        }
    }
}
